package com.seagox.chat.chat.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.seagox.chat.chat.entity.ChatContact;
import com.seagox.chat.chat.entity.Depart;
import com.seagox.chat.chat.entity.DepartUser;
import com.seagox.chat.chat.entity.BaseUser;
import com.seagox.chat.chat.mapper.ChatContactMapper;
import com.seagox.chat.chat.mapper.DepartMapper;
import com.seagox.chat.chat.mapper.DepartUserMapper;
import com.seagox.chat.chat.mapper.UserMapper;
import com.seagox.chat.chat.service.IUserService;
import com.seagox.chat.common.ResultCode;
import com.seagox.chat.common.ResultData;
import com.seagox.chat.template.UserModel;
import com.seagox.chat.util.EncryptUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserService implements IUserService {
    
    @Autowired
    private UserMapper userMapper;
    
    @Autowired
    private DepartUserMapper departUserMapper;
    
    @Autowired
    private ChatContactMapper chatContactMapper;
    
    @Autowired
    private DepartMapper departMapper;

	@Override
	public ResultData queryByDepartId(String departId) {
		return ResultData.success(userMapper.queryByDepartId(departId));
	}
	
	@Transactional
	@Override
	public ResultData insert(BaseUser user) {
		LambdaQueryWrapper<BaseUser> qw = new LambdaQueryWrapper<>();
		qw.eq(BaseUser::getPhone, user.getPhone()).in(BaseUser::getStatus, Arrays.asList(1,2));
		long count = userMapper.selectCount(qw);
		if(count == 0) {
			user.setPassword(EncryptUtils.hashpw("bit@" + user.getPhone().substring(7)));
			userMapper.insert(user);
			DepartUser departUser = new DepartUser();
			departUser.setUserId(user.getId());
			departUser.setDepartId(user.getDepartId());
			departUserMapper.insert(departUser);
			
			ChatContact chatContact = new ChatContact();
			chatContact.setClassify(3);
			chatContact.setFlag(1);
			chatContact.setMsgid("0");
			chatContact.setName("文件传输助手");
			chatContact.setSessionId("0");
			chatContact.setSource(user.getId());
			chatContact.setTarget("0");
			chatContact.setType(1);
			chatContact.setUnread(0);
			chatContactMapper.insert(chatContact);
			return ResultData.success(null);
		} else {
			return ResultData.warn(ResultCode.PARAMETER_ERROR, "手机号已存在");
		}
	}
	
	@Transactional
	@Override
	public ResultData edit(BaseUser user) {
		BaseUser oldUser = userMapper.selectById(user.getId());
		if(oldUser.getPhone().equals(user.getPhone())) {
			userMapper.updateById(user);
			LambdaQueryWrapper<DepartUser> uw = new LambdaQueryWrapper<>();
			uw.eq(DepartUser::getUserId, user.getId());
			DepartUser departUser = new DepartUser();
			departUser.setDepartId(user.getDepartId());
			departUserMapper.update(departUser, uw);
			
			LambdaQueryWrapper<ChatContact> uwContact = new LambdaQueryWrapper<>();
			uwContact.eq(ChatContact::getTarget, user.getId()).eq(ChatContact::getClassify, 1);
			ChatContact chatContact = new ChatContact();
			chatContact.setName(user.getName());;
			chatContactMapper.update(chatContact, uwContact);
			return ResultData.success(null);
		} else {
			LambdaQueryWrapper<BaseUser> qw = new LambdaQueryWrapper<>();
			qw.eq(BaseUser::getPhone, user.getPhone()).in(BaseUser::getStatus, Arrays.asList(1,2));
			long count = userMapper.selectCount(qw);
			if(count == 0) {
				userMapper.updateById(user);
				LambdaQueryWrapper<DepartUser> uw = new LambdaQueryWrapper<>();
				uw.eq(DepartUser::getUserId, user.getId());
				DepartUser departUser = new DepartUser();
				departUser.setDepartId(user.getDepartId());
				departUserMapper.update(departUser, uw);
				
				LambdaQueryWrapper<ChatContact> uwContact = new LambdaQueryWrapper<>();
				uwContact.eq(ChatContact::getTarget, user.getId()).eq(ChatContact::getClassify, 1);
				ChatContact chatContact = new ChatContact();
				chatContact.setName(user.getName());;
				chatContactMapper.update(chatContact, uwContact);
				return ResultData.success(null);
			} else {
				return ResultData.warn(ResultCode.PARAMETER_ERROR, "手机号已存在");
			}
		}
	}
	
	@Override
	public ResultData updatePassword(String userId, String oldPassword, String newPassword) {
		BaseUser user = userMapper.selectById(userId);
		if (!EncryptUtils.checkpw(oldPassword, user.getPassword())) {
			return ResultData.warn(ResultCode.PARAMETER_ERROR, "旧密码输入有误");
		} else if (EncryptUtils.checkpw(newPassword, user.getPassword())) {
			return ResultData.warn(ResultCode.PARAMETER_ERROR, "新密码不可以跟旧密码一样");
		} else {
			user.setPassword(EncryptUtils.hashpw(newPassword));
			user.setUpdateTime(new Date());
			userMapper.updateById(user);
			return ResultData.success(null);
		}
	}
	
	@Transactional
	@Override
	public ResultData updateAvatar(String userId, String avatar) {
		BaseUser user = userMapper.selectById(userId);
		user.setAvatar(avatar);
		userMapper.updateById(user);
		LambdaQueryWrapper<ChatContact> uw = new LambdaQueryWrapper<>();
		uw.eq(ChatContact::getTarget, userId).eq(ChatContact::getClassify, 1);
		ChatContact chatContact = new ChatContact();
		chatContact.setAvatar(avatar);
		chatContactMapper.update(chatContact, uw);
		return ResultData.success(null);
	}
	
	@Override
	public ResultData resetPassword(String id) {
		BaseUser user = userMapper.selectById(id);
		user.setPassword(EncryptUtils.hashpw("bit@" + user.getPhone().substring(7)));
		userMapper.updateById(user);
		return ResultData.success(null);
	}
	
	@Transactional
	@Override
	public void importHandle(List<UserModel> resultList) {
		for (UserModel userModel : resultList) {
			BaseUser user = new BaseUser();
			user.setEmail(userModel.getEmail());
			user.setPhone(userModel.getPhone());
			user.setName(userModel.getName());
			user.setPosition(userModel.getPosition());
			user.setSex(userModel.getSex());
			user.setPassword(EncryptUtils.hashpw("bit@" + userModel.getPhone().substring(7)));
			user.setSort(userModel.getSort());
			userMapper.insert(user);
			
			LambdaQueryWrapper<Depart> qw = new LambdaQueryWrapper<>();
			qw.eq(Depart::getCode, userModel.getCode());
			Depart depart = departMapper.selectOne(qw);
			DepartUser departUser = new DepartUser();
			departUser.setUserId(user.getId());
			departUser.setDepartId(depart.getId());
			departUserMapper.insert(departUser);
			
			ChatContact chatContact = new ChatContact();
			chatContact.setClassify(3);
			chatContact.setFlag(1);
			chatContact.setMsgid("0");
			chatContact.setName("文件传输助手");
			chatContact.setSessionId("0");
			chatContact.setSource(user.getId());
			chatContact.setTarget("0");
			chatContact.setType(1);
			chatContact.setUnread(0);
			chatContactMapper.insert(chatContact);
		}
	}

	@Override
	public ResultData delete(String ids) {
		LambdaQueryWrapper<BaseUser> uw = new LambdaQueryWrapper<>();
		uw.in(BaseUser::getId, Arrays.asList(ids.split(",")));
		BaseUser user = new BaseUser();
		user.setStatus(3);
		userMapper.update(user, uw);
		return ResultData.success(null);
	}

	@Override
	public ResultData update(BaseUser user) {
		userMapper.updateById(user);
		return ResultData.success(null);
	}
        
}
